#![allow(non_snake_case)]
use leptos::*;
use server_fn::ServerFnError;
use shq_common::prelude::{ CreditReportAbnormal, DpidUser, TransactionAbnormal};
use std::collections::HashMap;


//通过登录人部门id获取本部门员工
#[server(FetchUserByDpid,"/api")]
pub async fn fetch_user_by_dpid(dpid: u32) -> Result<Vec<DpidUser>,ServerFnError> {
    let res = shq_pas_backend::get_user_by_dpid(dpid).await.map_err(|e| {
        ServerFnError::new(format!("获取员工信息失败：{}", e))
    })?;
    Ok(res)
}

//保存家访员工数据
#[server(SaveHomeVisitUser,"/api")]
pub async fn save_home_visit_user(emids: Vec<u32>,dpid: u32,frid: u32) -> Result<(), ServerFnError> {
    use shq_pas_backend::save_home_visit_user;
    let res = save_home_visit_user(emids,dpid,frid).await.map_err(|e| {
        ServerFnError::new(format!("保存员工信息失败：{}", e))
    })?;
    Ok(res)
}

#[server(DelHomeVisitUser,"/api")]
pub async fn del_home_visit_user(id: u32) -> Result<(), ServerFnError> {
    use shq_pas_backend::del_home_visit_user;
    let res = del_home_visit_user(id).await.map_err(|e| {
        ServerFnError::new(format!("删除员工信息失败：{}", e))
    })?;
    Ok(res)
}

#[server(UpdateHomeVisitFileidToUser,"/api")]
pub async fn update_home_visit_fileid_to_user(id: u32,fileId: Vec<u64>) -> Result<(), ServerFnError> {
    use shq_pas_backend::update_home_visit_fileid_to_user;
    let res = update_home_visit_fileid_to_user(id,fileId).await.map_err(|e| {
        ServerFnError::new(format!("更新员工信息失败：{}", e))
    })?;
    Ok(res)
}

#[server(UpdateHomeVisitImageFileidToUser,"/api")]
pub async fn update_home_visit_image_fileid_to_user(id: u32,fileId: Vec<u64>) -> Result<(), ServerFnError> {
    use shq_pas_backend::update_home_visit_image_fileid_to_user;
    let res = update_home_visit_image_fileid_to_user(id,fileId).await.map_err(|e| {
        ServerFnError::new(format!("更新员工信息失败：{}", e))
    })?;
    Ok(res)
}

#[server(InitPassword, "/api/protect")]
pub async fn init_password(
    user_id: u32
) -> Result<(), ServerFnError> {
    shq_pas_backend::init_password(user_id)
        .await
        .or_else(|e| Err(ServerFnError::new(e.to_string())))?;
    Ok(())
}

#[server(FetchCreditReportAbnormal, "/api/protect")]
pub async fn fetch_credit_report_abnormal() -> Result<Vec<CreditReportAbnormal>, ServerFnError> {
    let res = shq_pas_backend::fetch_credit_report_abnormal()
        .await
        .or_else(|e| Err(ServerFnError::new(e.to_string())))?;
    Ok(res)
}

#[server(FetchTransactionAbnormal, "/api/protect")]
pub async fn fetch_transaction_abnormal() -> Result<Vec<TransactionAbnormal>, ServerFnError> {
    let res = shq_pas_backend::fetch_transaction_abnormal()
        .await
        .or_else(|e| Err(ServerFnError::new(e.to_string())))?;
    Ok(res)
}


#[server(SaveCreditReportAbnormalRecord, "/api/protect")]
pub async fn save_credit_report_abnormal_record(
    ny: u32,
    frid: u32,
    emid: u32,
    emno: String,
    emnm: String,
    abnormal_ids: Vec<u64>,
)-> Result<(), ServerFnError>{
    use shq_pas_backend::save_credit_report_abnormal_record;
    let res = save_credit_report_abnormal_record(ny, frid, emid, emno, emnm, abnormal_ids).await.map_err(|e| {
        ServerFnError::new(format!("保存员工信息失败：{}", e))
    })?;
    Ok(res)
}

#[server(SaveTransactionAbnormalRecord, "/api/protect")]
pub async fn save_transaction_abnormal_record(
    ny: u32,
    frid: u32,
    emid: u32,
    emno: String,
    emnm: String,
    abnormal_ids: Vec<u64>,
)-> Result<(), ServerFnError>{
    use shq_pas_backend::save_transaction_abnormal_record;
    let res = save_transaction_abnormal_record(ny, frid, emid, emno, emnm, abnormal_ids).await.map_err(|e| {
        ServerFnError::new(format!("保存员工信息失败：{}", e))
    })?;
    Ok(res)
}

#[server(FetchAbnormalRecordByFrid,"/api/protect")]
pub async fn fetch_abnormal_record_by_frid(frid: u32) -> Result<Vec<u32>,ServerFnError> {
    let res = shq_pas_backend::get_abnormal_record_by_frid(frid).await.map_err(|e| {
        ServerFnError::new(format!("获取异常记录信息失败：{}", e))
    })?;
    Ok(res)
}

#[server(FetchAbnormalTransactionByFrid,"/api/protect")]
pub async fn fetch_abnormal_transaction_by_frid(frid: u32) -> Result<Vec<u32>,ServerFnError> {
    let res = shq_pas_backend::get_abnormal_transaction_by_frid(frid).await.map_err(|e| {
        ServerFnError::new(format!("获取异常记录信息失败：{}", e))
    })?;
    Ok(res)
}

#[server(UpdateCreditReportFileidByFrid,"/api")]
pub async fn update_credit_report_fileid_by_frid(
    ny: u32,
    frid: u32,
    emid: u32,
    emno: String,
    emnm: String,
    fileids: Vec<u64>
) -> Result<(), ServerFnError> {
    use shq_pas_backend::update_credit_report_fileid_by_frid;
    let res = update_credit_report_fileid_by_frid(ny, frid, emid, emno, emnm, fileids).await.map_err(|e| {
        ServerFnError::new(format!("更新信息失败：{}", e))
    })?;
    Ok(res)
}

#[server(SaveTransactionFileByFrid,"/api")]
pub async fn save_transaction_file(
    ny: u32,
    frid: u32,
    emid: u32,
    emno: String,
    emnm: String,
    fileId: Vec<u64>,
) -> Result<(), ServerFnError> {
    use shq_pas_backend::save_transaction_file;
    let res = save_transaction_file(ny, frid, emid, emno, emnm, fileId).await.map_err(|e| {
        ServerFnError::new(format!("保存文件失败：{}", e))
    })?;
    Ok(res)
}

#[server(SaveCreditReportImageFileidByFrid,"/api")]
pub async fn save_credit_report_image_fileid_by_frid(
    ny: u32,
    frid: u32,
    emid: u32,
    emno: String,
    emnm: String,
    fileId: Vec<u64>
) -> Result<(), ServerFnError> {
    use shq_pas_backend::save_credit_report_image_fileid_by_frid;
    let res = save_credit_report_image_fileid_by_frid(ny, frid, emid, emno, emnm, fileId).await.map_err(|e| {
        ServerFnError::new(format!("保存信息失败：{}", e))
    })?;
    Ok(res)
}

#[server(FetchCreditReportImageByFrid,"/api/protect")]
pub async fn fetch_credit_report_image_by_frid(frid: u32) -> Result<HashMap<u64, u64>,ServerFnError> {
    let res = shq_pas_backend::get_credit_report_image_by_frid(frid).await.map_err(|e| {
        ServerFnError::new(format!("获取异常记录信息失败：{}", e))
    })?;
    Ok(res)
}

#[server(FetchTransactionFileByFrid,"/api/protect")]
pub async fn fetch_transaction_file_by_frid(frid: u32) -> Result<HashMap<u64, u64>,ServerFnError> {
    let res = shq_pas_backend::get_transaction_file_by_frid(frid).await.map_err(|e| {
        ServerFnError::new(format!("获取交易流水文件失败：{}", e))
    })?;
    Ok(res)
}

#[server(DelTransactionFileByFileid,"/api")]
pub async fn del_transaction_file_by_fileid(id: u64) -> Result<(), ServerFnError> {
    use shq_pas_backend::del_transaction_file_by_fileid;
    let res = del_transaction_file_by_fileid(id).await.map_err(|e| {
        ServerFnError::new(format!("删除文件失败：{}", e))
    })?;
    Ok(res)
}

#[server(DelCrediReportImageByFileid,"/api")]
pub async fn del_credit_report_image_by_fileid(id: u64) -> Result<(), ServerFnError> {
    use shq_pas_backend::del_credit_report_image_by_fileid;
    let res = del_credit_report_image_by_fileid(id).await.map_err(|e| {
        ServerFnError::new(format!("删除文件失败：{}", e))
    })?;
    Ok(res)
}

#[server(DelCreditReportFileByFileid,"/api")]
pub async fn del_credit_report_file_by_fileid(id: u32,frid: u32) -> Result<(), ServerFnError> {
    use shq_pas_backend::del_credit_report_file_by_fileid;
    let res = del_credit_report_file_by_fileid(id,frid).await.map_err(|e| {
        ServerFnError::new(format!("删除文件失败：{}", e))
    })?;
    Ok(res)
}

#[server(DelHomeVisitFileByFileid,"/api")]
pub async fn del_home_visit_file_by_fileid(id: u32,home_id: u32) -> Result<(), ServerFnError> {
    use shq_pas_backend::del_home_visit_file_by_fileid;
    let res = del_home_visit_file_by_fileid(id,home_id).await.map_err(|e| {
        ServerFnError::new(format!("删除文件失败：{}", e))
    })?;
    Ok(res)
}

#[server(DelHomeVisitImageByFileid,"/api")]
pub async fn del_home_visit_image_by_fileid(id: u32,home_id: u32) -> Result<(), ServerFnError> {
    use shq_pas_backend::del_home_visit_image_by_fileid;
    let res = del_home_visit_image_by_fileid(id,home_id).await.map_err(|e| {
        ServerFnError::new(format!("删除文件失败：{}", e))
    })?;
    Ok(res)
}

#[server(FetchFlowRunStep,"/api/protect")]
pub async fn fetch_flow_run_step(frid: u32,emid: u32) -> Result<u32,ServerFnError> {
    let res = shq_pas_backend::get_flow_run_step(frid,emid).await.map_err(|e| {
        ServerFnError::new(format!("获取流程节点信息失败：{}", e))
    })?;
    Ok(res)
}